package org.jeecg.modules.moduleApiImp;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.checkerframework.checker.units.qual.A;
import org.jeecg.common.constant.CommonCuckoo;
import org.jeecg.cuckoo.dao.CuUserDetailsDao;
import org.jeecg.cuckoo.moduleApi.ICuUserDetailsApi;
import org.jeecg.modules.entity.CuUserDetails;
import org.jeecg.modules.entity.CuVipCard;
import org.jeecg.modules.service.ICuUserDetailsService;
import org.jeecg.modules.service.ICuVipCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Date;

/**
 * 功能描述
 *
 * @author: wei
 * @date: 2024年07月11日 17:27
 */
@Service
public class ICuUserDetailsApiImp implements ICuUserDetailsApi {

    @Autowired
    private ICuUserDetailsService cuUserDetailsService;

    @Autowired
    private ICuVipCardService cuVipCardService;

    @Override
    public void registerUser(CuUserDetailsDao user) {
        CuUserDetails userDetails = JSONUtil.toBean(JSONUtil.toJsonStr(user), CuUserDetails.class);
        // 业务处理 邀请码已带入
        userDetails.setIsVip(CommonCuckoo.IS_NOT_VIP);
        userDetails.setEndVip(new Date());
        String InvitationCode = cuUserDetailsService.generateInvitationCode();
        userDetails.setInvitCode(InvitationCode);

        // 默认注册送卷
        // 先查出 默认的卷
        CuVipCard vipCard = new CuVipCard();
        vipCard.setCode("0000");
        QueryWrapper<CuVipCard> vipCardQueryWrapper = new QueryWrapper<>(vipCard);
        vipCard = cuVipCardService.getOne(vipCardQueryWrapper);
        if (ObjectUtil.isNotEmpty(vipCard) && vipCard.getDaysNumber() > 0) {
            // 当前时间 + 天数
            Date endVip = DateUtil.offsetDay(new Date(), vipCard.getDaysNumber());
            userDetails.setEndVip(endVip);
            userDetails.setIsVip(CommonCuckoo.IS_VIP);
        }

        // 不管存不存在都要删除
        CuUserDetails cu = new CuUserDetails();
        cu.setUserName(user.getUserName());
        QueryWrapper<CuUserDetails> userDetailsQueryWrapper = new QueryWrapper<>(cu);
        cuUserDetailsService.remove(userDetailsQueryWrapper);

        // 删除后再添加 覆盖创建时间
        cuUserDetailsService.save(userDetails);
    }

    @Override
    public boolean checkRegisterCode(String registerCode) {
        // 检测注册码
        CuUserDetails cuReg = new CuUserDetails();
        cuReg.setInvitCode(registerCode);
        QueryWrapper<CuUserDetails> cuRegWrapper = new QueryWrapper<>(cuReg);
        cuReg = cuUserDetailsService.getOne(cuRegWrapper);
        if (ObjectUtil.isEmpty(cuReg)) {
            return false;
        }
        return true;
    }

    @Override
    public CuUserDetailsDao getVipInfo(String userId) {
        CuUserDetails details = cuUserDetailsService.getById(userId);
        if (ObjectUtil.isEmpty(details)) {
            return null;
        }
        return JSONUtil.toBean(JSONUtil.toJsonStr(details), CuUserDetailsDao.class);
    }
}
